<?php


namespace App\Http\Controllers\Internal;


use App\Models\UserCourse;
use App\Models\UserStatus;

class CourseController extends Controller
{
    /*
     * @api post /internal/courses 购买成功后记录购买的课程
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'courses' => 'required|array',
            'userId' => 'required|integer',
            'orderId' => 'required|integer',
        ]);
        $userId = $this->request->input('userId');
        $orderId = $this->request->input('orderId');
        $courses = $this->request->input('courses');
        foreach ($courses as $course) {
            // 可能有购买的和体验的
            /** @var UserCourse $courseObj */
            $courseObj = UserCourse::ofUserCourse($userId, $course['courseId'])->first();
            if ($courseObj) {
                $courseObj->setOrderId($orderId);
                $courseObj->setStatus(UserCourse::IS_TRUE);
            } else {
                $courseObj = new UserCourse();
                $courseObj->setUserId($userId);
                $courseObj->setOrderId($orderId);
                $courseObj->setLevel($course['level']);
                $courseObj->setCourseId($course['courseId']);
            }
            $courseObj->save();
        }
        return $this->sendSuccessJson();
    }

    /*
     * @api get /internal/courses/current 获取用户当前的课程
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function current()
    {
        $this->validate($this->request, [
            'userId' => 'required|integer',
        ]);
        $userId = $this->request->input('userId');
        $data = [];
        $courseObj = UserCourse::getCurrent($userId);
        if ($courseObj) {
            $data = [
                'courseId' => $courseObj->getCourseId(),
                'isEnd' => $courseObj->getIsEnd(),
                'level' => $courseObj->getLevel(),
            ];
        }
        return $this->sendSuccessJson($data);
    }

    /*
     * @api get /internal/courses/currents 获取所有用户当前的课程
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function currents()
    {
        $this->validate($this->request, [
            'page' => 'required|integer',
            'limit' => 'required|integer',
        ]);
        $page = $this->request->input('page');
        $limit = $this->request->input('limit');
        $isEnd = $this->request->input('isEnd');
        $userCourseObj = UserCourse::getAllUsersCurrent($isEnd, $page, $limit);

        $items = [];
        /** @var UserCourse $o */
        foreach($userCourseObj as $o) {
            $items[$o->getId()] = [
                'id' => $o->getId(),
                'userId' => $o->getUserId(),
                'courseId' => $o->getCourseId(),
                'level' => $o->getLevel(),
                'isEnd' => $o->getIsEnd()
            ];
        }
        return $this->sendSuccessJson($items);
    }

    /*
     * @api post /internal/courses/pushend 设置用户课程is_end字段为推送结束
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function pushEnd()
    {
        $this->validate($this->request, [
            'userId' => 'required|integer',
            'courseId' => 'required|integer',
        ]);
        $userId = $this->request->input('userId');
        $courseId = $this->request->input('courseId');

        $courseObj = UserCourse::ofUserCourse($userId, $courseId)->first();
        $courseObj->setIsEnd(UserCourse::IS_END['push']);
        $courseObj->save();

        return $this->sendSuccessJson();
    }

    /*
     * @api get /internal/courses/next-level 获取用户下一个level的课程信息
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function nextLevel()
    {
        $this->validate($this->request, [
            'userId' => 'required|integer',
            'level' => 'required|integer',
        ]);
        $userId = $this->request->input('userId');
        $level = $this->request->input('level');
        /** @var UserCourse $res */
        $res = UserCourse::where('user_id', $userId)->where('level', $level+1)->first();
        $data = [];
        if ($res) {
            $data = [
                'id' => $res->getId(),
                'courseId' => $res->getCourseId()
            ];
        }
        return $this->sendSuccessJson($data);
    }

    /*
     * @api get /internal/courses/starttimelearn 获取用户最早的学习开始时间
     * @version 0.0.1
     * @group internal
     *
     * @param body int userId 用户Id
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function startTimeLearn()
    {
        $this->validate($this->request, [
            'userId' => 'required|integer',
        ]);
        $userId = $this->request->input('userId');
        $res = UserStatus::getStudyTime($userId);
        return $this->sendSuccessJson($res);
    }

}
